# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=onevpl-cpu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2022.2.4
pkgrel=1
pkgdesc='oneVPL runtime implementation for CPU (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.somepackage.org/'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-onevpl"
             'git')
source=("https://github.com/oneapi-src/oneVPL-cpu/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-disable-mingw-workaround.patch
        0002-bootstrap-fix-ffmpeg-command.patch
        0003-cmake-remove-unknown-options.patch
        0004-cmake-add-bcrypt-lib.patch)
sha256sums=('5a236a0c366db38d8d2e64e2df2e42f355b8a46175e78977f9f150d2d5bce81c'
            'e805e850b4827fe15c77a006207183f25aae1cdffb25856988100a8be834ce65'
            '1f495f6a5cee0ff6b8265288697a535cd08f6b0f113f2c560e7835f6044ec601'
            'c337ee9c8c86f3479d382c854f4fc7a88cf9bcf2e6d928ce679cba2f468a7596'
            'bd9c18382ae029b4b5b36dd8efc0735a2be3320c3a535429369276ba412cf15d')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/0001-disable-mingw-workaround.patch
  patch -p1 -i "${srcdir}"/0002-bootstrap-fix-ffmpeg-command.patch
  patch -p1 -i "${srcdir}"/0003-cmake-remove-unknown-options.patch

  # https://github.com/oneapi-src/oneVPL-cpu/pull/41
  patch -p1 -i "${srcdir}"/0004-cmake-add-bcrypt-lib.patch
}

build() {
  local -x PKG_CONFIG_LIBDIR="${srcdir}/deps-install/lib"
  local -x VPL_CPU_DEPS_BUILD_DIR="${srcdir}/deps-build"
  local -x VPL_BUILD_DEPENDENCIES="${srcdir}/deps-install"

  "oneVPL-cpu-${pkgver}/script/bootstrap" gpl

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_PREFIX_PATH="${srcdir}/deps-install" \
      -DBUILD_GPL_X264=ON \
      -DBUILD_TESTS=OFF \
      -DONEAPI_INSTALL_LICENSEDIR="share/licenses/${_realname}" \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}
